package com.wwj.airline.Controller;

import com.wwj.airline.Service.UserService;
import com.wwj.airline.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public ModelAndView login(){
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    @PostMapping("/login/user")
    public ModelAndView login(String email, String password, HttpServletRequest request){
        //System.out.println(email);
        List<User> users = userService.login(email);
        if(users != null){
            for (User user : users) {
                if(user.getPassword().equals(password)){
                    System.out.println(email);
                    request.getSession().setAttribute("user", user);
                    request.getSession().setAttribute("userId", user.getId());
                    //用户名存入该用户的session 中
                    ModelAndView modelAndView = new ModelAndView("redirect:/");
                    modelAndView.addObject("loginMessage","已成功登入");
                    return modelAndView;
                }
            }
        }
        ModelAndView modelAndView = new ModelAndView("redirect:/login");
        modelAndView.addObject("loginMessage","用户名或密码不正确");
        return modelAndView;
    }


    @GetMapping("/user/{id}")
    public ModelAndView showUser(@PathVariable Integer id){
        ModelAndView modelAndView = new ModelAndView("user");
        modelAndView.addObject("user",userService.getUser(id));
        return modelAndView;
    }

    @GetMapping("/user/book")
    public ModelAndView showBook(){
        ModelAndView modelAndView = new ModelAndView("book");
        //modelAndView.addObject("user",userService.getUser(id));
        return modelAndView;
    }

    @GetMapping("/register")
    public ModelAndView showRegister(){
        ModelAndView modelAndView = new ModelAndView("register");
        //modelAndView.addObject("user",userService.getUser(id));
        return modelAndView;
    }

    @PostMapping("/user/register")
    public ModelAndView register(String username,String password,String sex,String identification,String email,String phone){
        User user = new User(username,password,sex,identification,email,password);
        userService.insertUser(user);
        ModelAndView modelAndView = new ModelAndView("login");
        //modelAndView.addObject("user",userService.getUser(id));
        return modelAndView;
    }

    @PutMapping("/user/update/{id}")
    public void updateUser(@PathVariable Integer id,User user){
        user.setId(id);
        userService.updateUser(user);
    }


}
